home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!milod
- From: milod@netcom.com (John DiCamillo)
- Subject: Re: Interpreting tokens in C++???
- Message-ID: <milodDMu3qJ.B96@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <312238A1.4F70@cadman.cit.buffalo.edu>
- Date: Thu, 15 Feb 1996 20:32:43 GMT
- Sender: milod@netcom16.netcom.com
-
- nvp@acsu.buffalo.edu writes:
-
- >Hello, I am trying to write C++ code that considers a theoretical
- >language (TLAN), and through two classes (and re-casting), interprets
- >tokens from the TLAN to have meaning in C++.
-
- >BTW, these programs are not being compiled (no error checking for now),
- >but are interpreted by the code.
-
- OK, so you're writing a simple interpreter.
- Why just two classes? Is that a constraint of the
- homework problem? (BTW, is this a compiler class
- or a computer architecture class?)
-
- >Here's a sample TLAN program:
-
- >BEGIN // ignored by interpreter.
- >SET I 10 // i = 10
- >SET V 1 // v = 1
- >WHILE V
- > BEGIN
- > INC V // v++
- > DEC I // i--
- > PRINT V // cout << v
- >NEWLINE // endl
- >END
- >END // program end
-
- >I am presently using #define to define each token (using an integer)
- >and strtok (from the c-library token.h).
-
- Here, I suspect you have your terminology confused.
- I think you are using #define to define token *types*.
- In other words, "#define SET 10" defines a token
- type SET. The words in the program text that are
- equal to "SET" are the *tokens*, and they are instances
- of the token type SET.
-
- >For a statement like SET I 10, how would I get token I to = token 10?
-
- >Strtok breaks them up into tokens, but I am having problems with
- >interpreting the meaning of statements after that ...
-
- Well, you have a couple ways to go, depending on whether
- you will be using this program as the basis for future
- work. The right way to do things is to define a lexicon
- and grammar for TLAN and build a parser and scanner using
- lex and yacc (or equivalent -- I prefer to use object-
- oriented scanning and parsing classes, as described in
- http://www.qds.com/people/jdicamillo/oosp.htm). Then you
- use these tools to construct an abstract syntax tree from
- the program text and interpret that.
-
- The quicker way to do this (given that TLAN is tiny and you
- probably are going to learn the other stuff later anyways)
- is to define a class "Variable" or "Symbol" that you can use
- to represent program variables. This class has a string
- name, and an integer value. Then when your interpreter
- sees the statement "SET I 10", it creates a new Variable by
- supplying the name "I" and the value 10. Hint: you probably
- want to store all of your Variable in an environment or
- symbol table for easy look-up later.
-
- Then your only problem is dealing with WHILE-BEGIN-END.
- Oh well, at least you don't have scoping and closures to deal
- with...yet. :-)
-
- >Nathan V. Patwardhan
- >nvp@cs.buffalo.edu
-
- --
- ciao,
- milo
- ================================================================
- John DiCamillo Fiery the Angels Fell
- milod@netcom.com Deep thunder rode around their shores
-